<?php 
/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                     (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it 
    under the terms of the GNU General Public License (version 2) as published 
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with 
    Amrita ITEWS if not then it can be viewed here: 
    http://itews.amrita.ac.in/license.html.
    
    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/
 
    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews"; 
    
    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa 
    Vidyapeetham as part of the project titled,\"Intelligent & Interactive 
    Telematics using Emerging Wireless Technologies for Transport Systems 
    (ITEWS)\" sponsored by Technology Information Forecasting and Assessment 
    Council (TIFAC), India.
***/
?><?php
// Setting the Content-Type header with charset
header('Content-Type: text/html; charset=utf-8');
require_once("./utils.php.inc");
require_once("./settings.php.inc");

session_start();


if (isset($_GET['passengerLangCode'])){
	//store it in the cookie
	
	//store it in the session variable
	$_SESSION['currentPassengerLanguage'] = $_GET['passengerLangCode']; // store session data

}

//access session variable...if not set, then check the cookie or else default
$currentPassengerLanguage = isset($_SESSION['currentPassengerLanguage'])?$_SESSION['currentPassengerLanguage']:DEFAULT_LANG_FOR_PASSENGER_INTERFACE;//add logic for cookie read
$_SESSION['currentPassengerLanguage']= $currentPassengerLanguage;

?>
<head>
<style type="text/css">
span  {font-size:15pt}
td,b {font-size:15pt}
body
{
  overflow: hidden;
}
</style>
<style type="text/css">
<?php
// Setting the Content-Type header with charset
echo "body {
    font: Verdana,Arial,sans-serif;
	 /* An explicit background color is required for Safari. */
	 /* Otherwise your corner chunks will come out black!    */
	background:  #000055;
}";

$blanks = ' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ';
?>
</style>
<script type="text/javascript" src="jquery-1.2.3.js"></script>
<!--script type="text/javascript" src="jquery.timers.js"></script-->
<script type="text/javascript" src="jquery.timer.js"></script>
	<script type="text/javascript">
	   //$(document).ready(
		//$($.everyTime(3000,function(){
		//$($.timer(3000, function (timer) {
		$(function() {
			//alert("***");
			
			//HIDE all footers first
			$('#next_stn').css('display','none');
			$('#final_dstn').css('display','none');
			$('#curr_stn').css('display','none');
			$('#next_stn_lbl').css('display','none');
			$('#final_dstn_lbl').css('display','none');
			$('#curr_stn_lbl').css('display','none');


			var current_language = '<?php echo $currentPassengerLanguage;?>';
			
			$.timer(3000, function (timer) {
			var exit_var = false;
			//var counter = 0;	
				//counter++;
				$.ajax({
				    url: './webticker_xml.php',
				    type: 'GET',
				    dataType: 'xml',
				    timeout: 10000,
				    error: function(){
				        //alert('Error loading XML document'); EAT IT
				    },
				    success: function(xml){

						$(xml).find('CurrentLanguageCode').each(function(){
							var item_text = $(this).text();
							if (item_text!=current_language){
								top.frames[1].location.reload();
								exit_var=true;
							}
						});
						if (!exit_var){
							
							$(xml).find('RouteChanged').each(function(){
								var item_text = $(this).text();
								if (item_text=="true"){
									top.window.location.reload();
									exit_var=true;
								}
							});
							if (!exit_var){
								$(xml).find('CurrentRouteCode').each(function(){
									var item_text = $(this).text();
									//alert(item_text.length);
									if (item_text.length==0){
										$('#next_stn').css('display','none');
										$('#final_dstn').css('display','none');
										$('#curr_stn').css('display','none');
										$('#next_stn_lbl').css('display','none');
										$('#final_dstn_lbl').css('display','none');
										$('#curr_stn_lbl').css('display','none');
										exit_var=true;
									}
								});
								
								if (!exit_var){
									//if(0==counter){
										$('#next_stn').css('display','inline');
										$('#final_dstn').css('display','inline');
										$('#next_stn_lbl').css('display','inline');
										$('#final_dstn_lbl').css('display','inline');
									//}


									
									$(xml).find('NextStation').find('name').each(function(){
							        var item_text = $(this).text();
									//alert(item_text);
									if (item_text==""){
										$('#next_stn').css('display','none');
										$('#next_stn_lbl').css('display','none');
									}
									else{
										$('#next_stn').css('display','inline');
										$('#next_stn_lbl').css('display','inline');
								        $('#next_stn')
								            .html(item_text);
							            //.appendTo('ol');
									}
									});
									
									$(xml).find('FinalDestination').find('name').each(function(){
							        var item_text = $(this).text();
									//alert(item_text);
							        $('#final_dstn')
							            .html(item_text);
							            //.appendTo('ol');
									});

									currently_in_station = false;
									
									$(xml).find('CurrentStation').find('name').each(function(){
							        var item_text = $(this).text();
							        $('#curr_stn')
							            .html(item_text);
							            //.appendTo('ol');
									currently_in_station = true;
									});

									if (currently_in_station){
										$('#curr_stn_lbl').css('display','inline');
										$('#curr_stn').css('display','inline');
										
									}
									else {
										$('#curr_stn_lbl').css('display','none');
										$('#curr_stn').css('display','none');
									}
								}
							}
						}
				    }	
				});
			});
		});
  	</script>
</head>
  <BODY leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
    <!--DIV ID="TICKER" STYLE="overflow:hidden; width:520px">
      My First Ticker!
    </DIV>
    <script type="text/javascript" src="webticker_lib.js"></script-->
	<div id="TICKER" style="filter:alpha(opacity=100);-moz-opacity:1.0;border-top: 0px solid rgb(204, 204, 204); border-bottom: 0px solid rgb(204, 204, 204); overflow: hidden; display: block; background-color: rgb(0, 0, 85); width: 100%;" onmousedown="TICKER_PAUSED=true" onmouseup="TICKER_PAUSED=false"><table cellpadding="0" cellspacing="0" width="100%">
	<tbody><tr><td nowrap="nowrap">
<?php

//////////////////////////////////////////////////////////////
//
//
//
//	Check whether current location sensing is gps or wifi
//		- look for file .gps_ls or .wifi_ls
//
///////////////////////////////////////////////////////////////
//echo "??? ????? ????"."<BR>";

$error_occured=false;

$sql = "SET NAMES utf8";
if ( ($result = query_wrapper($sql, __FILE__, __LINE__))==null){
	$error_occured = true;
}
else {
	$sql = "SELECT s.str_code as strCode, ls.localized_str as localStr, ls.lang_code as langCode FROM localized_strings ls, context_strings cs, strings s WHERE ls.lang_code = '".$currentPassengerLanguage."' AND cs.ctxt_code = 'tkr' AND cs.str_code = s.str_code AND cs.id = ls.context_strings_id";
	if ( ($result = query_wrapper($sql, __FILE__, __LINE__))==null){
		$error_occured = true;
	}
	else {
		if (mysql_num_rows($result)==0){
			$sql = "SELECT s.str_code as strCode, ls.localized_str as localStr, ls.lang_code as langCode FROM localized_strings ls, context_strings cs, strings s WHERE ls.lang_code = '".DEFAULT_LANG_FOR_PASSENGER_INTERFACE."' AND cs.ctxt_code = 'tkr' AND cs.str_code = s.str_code AND cs.id = ls.context_strings_id";
			if ( ($result = query_wrapper($sql, __FILE__, __LINE__))==null){
				$error_occured = true;
			}
		}
	}
}

if(!$error_occured){
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		switch($row["strCode"]){
			case "nxt_stn":
				$next_station = $row["localStr"];
				break;
			case "fnl_dst":
				$final_destination = $row["localStr"];
				break;
			case "nrb_pls":
				$nearby_places = $row["localStr"];
				break;
			case "cur_stn":
				$current_station = $row["localStr"];
				break;
			default:
		}
	}
}
/****************************
//--Next station value
$sql = 'select nn.node_name from route_schedule_node_assoc rsna , node_names nn , persistent_data pd '
        . ' WHERE '
        . ' pd.current_route_code = rsna.route_schedule_code '
        . ' AND rsna.node_seq_no = ( pd.current_node_seq_no + 1 ) '
        . ' AND rsna.node_code = nn.node_code '
        . ' AND nn.lang_code = \''.$currentLanguage.'\' LIMIT 1';
if ( ($result_ns = query_wrapper($sql, __FILE__, __LINE__))==null){
	$error_occured = true;
}
else {
	if (mysql_num_rows($result_ns)==0){
		$sql = 'select nn.node_name from route_schedule_node_assoc rsna , node_names nn , persistent_data pd '
        . ' WHERE '
        . ' pd.current_route_code = rsna.route_schedule_code '
        . ' AND rsna.node_seq_no = ( pd.current_node_seq_no + 1 ) '
        . ' AND rsna.node_code = nn.node_code '
        . ' AND nn.lang_code = \'eng\' LIMIT 1';
		
		if ( ($result_ns = query_wrapper($sql, __FILE__, __LINE__))==null){
			$error_occured = true;
		}
	}
}

if(!$error_occured){
	while ($row_ns = mysql_fetch_array($result_ns, MYSQL_ASSOC)) {
		$next_station_value = $row_ns["node_name"];
	}
}
else {
	$next_station_value = "---";
}

//--Final destinantion value
$sql = 'select nn.node_name from route_schedule_node_assoc rsna , node_names nn, persistent_data pd'
        . ' WHERE'
        . ' pd.current_route_code = rsna.route_schedule_code'
        . ' AND rsna.node_code = nn.node_code'
        . ' AND nn.lang_code = \''.$currentLanguage.'\''
        . ' ORDER BY rsna.node_seq_no DESC'
        . ' LIMIT 1';
if ( ($result_fd = query_wrapper($sql, __FILE__, __LINE__))==null){
	$error_occured = true;
}
else {
	if (mysql_num_rows($result_fd)==0){
		$sql = 'select nn.node_name from route_schedule_node_assoc rsna , node_names nn, persistent_data pd'
		        . ' WHERE'
		        . ' pd.current_route_code = rsna.route_schedule_code'
		        . ' AND rsna.node_code = nn.node_code'
		        . ' AND nn.lang_code = \'eng\''
		        . ' ORDER BY rsna.node_seq_no DESC'
		        . ' LIMIT 1';
		if ( ($result_fd = query_wrapper($sql, __FILE__, __LINE__))==null){
			$error_occured = true;
		}
	}
}

if(!$error_occured){
	while ($row_fd = mysql_fetch_array($result_fd, MYSQL_ASSOC)) {
		$final_destination_value = $row_fd["node_name"];
	}
}
else {
	$final_destination_value = "---";
}


//--Current station value
$currently_at_station = false;
$sql = 'select current_node_code from persistent_data'
        . ' LIMIT 1';
if ( ($result = query_wrapper($sql, __FILE__, __LINE__))==null){
	$error_occured = true;
}
else {
	if (mysql_num_rows($result)> 0){//TODO...
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$current_station_code = trim($row["current_node_code"]);
		}
		if (strcmp($current_station_code,"")!=0){//means the vehicle is currently at a station
			$currently_at_station = true;
			$sql = 'select node_name from node_names'
			        . ' WHERE'
			        . ' node_code = \''.$current_station_code.'\''
					. ' AND lang_code = \''.$currentLanguage.'\'';
					
			if ( ($result = query_wrapper($sql, __FILE__, __LINE__))==null){
				$error_occured = true;
			}
			else {
				if (mysql_num_rows($result)==0){
					$sql = 'select node_name from node_names'
					        . ' WHERE'
					        . ' node_code = \''.$current_station_code.'\''
							. ' AND lang_code = \'eng\'';
					if ( ($result = query_wrapper($sql, __FILE__, __LINE__))==null){
						$error_occured = true;
					}
				}
			}
		}
	}
}

if($currently_at_station && !$error_occured){
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$current_station_value = $row["node_name"];
	}
}
else {
	$current_station_value = "---";
}
***************************************************************************************/
?>
		<span style="font-family: Arial; font-size: 12px; color: rgb(68, 68, 68);" id="TICKER_BODY" width="100%">
		

			<span style="background-color: #005"> <?php echo $blanks; ?></span> 

			<!-- MESSAGE: Next station: ___ Kms -->
			<span style="background-color: rgb(127, 181, 26);" id="next_stn_lbl"> 
					&nbsp; &nbsp; <font color="#ffffff"> 
						<?php echo $next_station; ?>
					</font>&nbsp; &nbsp; 
			</span> &nbsp; 
			
			
				<span style="color: rgb(255, 255, 255);" id="next_stn"><?php //echo $next_station_value; ?></span>
			&nbsp; 

			<!-- MESSAGE: Final Destination: ____ Kms -->
			<span style="background-color: rgb(255, 170, 0);" id="final_dstn_lbl"> 
				&nbsp; &nbsp; <font color="#ffffff"> 
					<?php echo $final_destination; ?>
				</font>&nbsp; &nbsp; 
			</span> &nbsp; 
			
			
				<span style="color: rgb(255, 255, 255);" id="final_dstn"><?php //echo $final_destination_value; ?></span>
			&nbsp; 

			<!-- MESSAGE: Nearby places: ____ -->
			<!--span style="background-color: rgb(200, 200, 0);"> 
				&nbsp; &nbsp; <font color="#ffffff"> 
					<b><?php //echo $nearby_places; ?></b>
				</font>&nbsp; &nbsp; 
			</span> &nbsp; 
			
			<b style="color: rgb(200, 200, 0)">
				India, US to move forward on space cooperation ... Private players allowed to bid for Hydro project
			</b-->

			<!-- MESSAGE: Current Station: ____ -->
			<span style="background-color: rgb(0, 136, 255); display:none"  id="curr_stn_lbl"> 
				&nbsp; &nbsp; <font color="#ffffff"> 
					<?php echo $current_station; ?>
				</font>&nbsp; &nbsp; 
			</span> &nbsp; 
			
			<span id="curr_stn" style="color: rgb(255, 255, 255); display:none"> <?php //echo $current_station_value; ?> </span>

			<span style="background-color: #005"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
			</span>

		</span>
			
	</td></tr></tbody></table></div>

	<script type="text/javascript" src="webticker_lib.js" language="javascript"></script>
	
	
  </BODY>
